import pyautogui
import time
import pyperclip
import os, stat,shutil
print('程序开始')
dir='C:/Users/Public/making-the-tender/word格式处理'
handle_dir=dir+'/待处理'
ok_dir=dir+'/已处理'
filenames=os.listdir(handle_dir)
for filename in filenames:
    if not (filename.endswith('.doc') or filename.endswith('.docx')):
        print('文件格式错误：'+filename)
        continue
    name=handle_dir+'/'+filename
    print('文件：'+name)
    pyperclip.copy(name)
    #打开文件系统
    pyautogui.hotkey('winleft','e')
    #跳转到对应目录
    time.sleep(2)
    pyautogui.hotkey('tab')
    time.sleep(0.5)
    pyautogui.hotkey('tab')
    time.sleep(0.5)
    pyautogui.hotkey('tab')
    pyautogui.hotkey('enter')
    time.sleep(0.5)
    pyautogui.hotkey('ctrlleft','v')
    time.sleep(0.5)
    pyautogui.hotkey('enter')
    time.sleep(5)
    print('全选内容')
    pyautogui.hotkey('ctrlleft','a')
    time.sleep(2)
    print('设置字体')
    pyautogui.hotkey('ctrlleft','d')
    pyperclip.copy('华文楷体')
    time.sleep(1)
    pyautogui.hotkey('ctrlleft','v')
    time.sleep(0.2)
    pyperclip.copy('四号')
    pyautogui.hotkey('tab')
    pyautogui.hotkey('tab')
    time.sleep(0.2)
    pyautogui.hotkey('ctrlleft','v')
    pyautogui.hotkey('enter')
    time.sleep(1)
    print('设置段落')
    pyautogui.hotkey('altleft','o','p')
    time.sleep(1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置对齐方式')
    pyautogui.hotkey('pageup')
    time.sleep(0.1)
    pyautogui.hotkey('down')
    pyautogui.hotkey('down')
    pyautogui.hotkey('down')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置大纲级别')
    pyautogui.hotkey('pageup')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置方向')
    pyautogui.hotkey('left')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置缩进前')
    pyautogui.typewrite('0')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置缩进后')
    pyautogui.typewrite('0')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置特殊格式')
    pyautogui.hotkey('pageup')
    time.sleep(0.1)
    pyautogui.hotkey('down')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置度量值')
    pyautogui.typewrite('2')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置间距前')
    pyautogui.typewrite('0')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置间距后')
    pyautogui.typewrite('0')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    print('设置间行距')
    pyautogui.hotkey('pageup')
    time.sleep(0.1)
    pyautogui.hotkey('down')
    pyautogui.hotkey('down')
    pyautogui.hotkey('down')
    pyautogui.hotkey('down')
    time.sleep(0.1)
    pyautogui.hotkey('tab')
    time.sleep(0.1)
    pyautogui.typewrite('28')
    time.sleep(0.1)
    pyautogui.hotkey('enter')
    time.sleep(3)
    print('保存')
    pyautogui.hotkey('left')
    time.sleep(0.1)
    pyautogui.hotkey('ctrlleft','s')
    time.sleep(1)
    pyautogui.hotkey('y')
    time.sleep(0.1)
    pyautogui.hotkey('backspace')
    time.sleep(4)
    print('关闭')
    pyautogui.hotkey('altleft','f4')
    time.sleep(2)
    print('移动文件位置')
    try:
        shutil.move(name,ok_dir+'/'+filename)
        time.sleep(0.5)
        pyautogui.hotkey('altleft','f4')
        time.sleep(0.5)
    except Exception as err:
        print(err)
        time.sleep(0.5)
        pyautogui.hotkey('altleft','f4')
        time.sleep(0.5)
    #time.sleep(1000000)
print('程序结束')